package com.fz.us.oa.web.quartz;

import com.fz.us.admin.base.bean.BaseEnumManager;
import com.fz.us.admin.base.utils.DataUtil;
import com.fz.us.admin.base.utils.LogUtil;
import com.fz.us.admin.base.utils.StringUtils;
import com.fz.us.admin.company.bean.FlowEnum;
import com.fz.us.admin.company.entity.Remind;
import com.fz.us.admin.company.entity.User;
import com.fz.us.admin.company.service.RemindService;
import com.fz.us.admin.company.service.UserService;
import com.fz.us.oa.core.entity.com.*;
import com.fz.us.oa.core.service.com.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * Created by ZhuChunXiao on 2017/5/26.
 */
public class JobImpl implements Job {
    @Autowired
    private PersonnelInfoService personnelInfoService;
    @Autowired
    private PersonnelUseService personnelUseService;
    @Autowired
    private RemindService remindService;
    @Autowired
    private UserService usersService;
    @Autowired
    private PersonnelSelfService personnelSelfService;
    @Autowired
    private CertificateService certificateService;
    @Autowired
    private TaskService taskService;

    @Override
    public void calNeLiveValidate() {
        System.out.println("aaaaaaaaaaaaaaaaaaa");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("processState", FlowEnum.ProcessState.Finished);
        List<PersonnelSelf> personnelselfList = personnelSelfService.getList(map);
        System.out.println("###" + personnelselfList.size());
        Date today = new Date();
//        for(PersonnelSelf personnelSelf:personnelselfList){
        for(int i=0;i<personnelselfList.size();i++){
            PersonnelSelf personnelSelf=personnelselfList.get(i);
            System.out.println("###"+personnelSelf.getAddress());
            if(StringUtils.isNotEmpty(personnelSelf.getUserId())&&personnelSelf.getReDateEnd()!=null&&DataUtil.DateDiff(today, (DataUtil.getAfterDay(personnelSelf.getReDateEnd(), -30)))==0){

                //userId 获取这个员工
                User emp=usersService.get(personnelSelf.getUserId().trim());
                if(!StringUtils.equals(emp.getState().name(),"Delete")) {

                    //personnelUserString 获取此员工对应的人事专员
                    for (String userId : personnelSelf.getPersonnelUserString().split(",")) {
                        if (usersService.get(userId) != null) {
                            //发送提醒
                            Remind remind = new Remind();
                            remind.setUser(usersService.get(userId));
                            remind.setBussinessKey("personnelSelf");
                            remind.setKeyId(personnelSelf.getId());
                            remind.setContent(emp.getName() + "的居住证即将到期，请查看");
                            remindService.save(remind);
                        }
                    }
                }
            }
        }
    }

    @Override
    public void calWorkValidate() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("processState", FlowEnum.ProcessState.Finished);
        List<PersonnelUse> personneluseList = personnelUseService.getList(map);
        Date today = new Date();
//        for(PersonnelUse personnelUse:personneluseList){
        for(int i=0;i<personneluseList.size();i++){
            PersonnelUse personnelUse=personneluseList.get(i);
            if(StringUtils.isNotEmpty(personnelUse.getUserId()) &&(DataUtil.DateDiff(today,(DataUtil.getAfterDay(personnelUse.getContractDateEnd(), -30)))==0||today.after(DataUtil.getAfterDay(personnelUse.getTrialDateEnd(),-30)))){

                //获取这个员工
                User emp=usersService.get(personnelUse.getUserId().trim());
                if(!StringUtils.equals(emp.getState().name(),"Delete")) {

                    //personnelUserString 获取此员工对应的人事专员
                    for(String userId:personnelUse.getPersonnelUserString().split(",")){
                        if(usersService.get(userId)!=null){
                            //发送提醒
                            Remind remind = new Remind();
                            remind.setUser(usersService.get(userId));
                            remind.setBussinessKey("personnelUse");
                            remind.setKeyId(personnelUse.getId());
                            remind.setContent(emp.getName() + "的用工即将到期，请查看");
                            remindService.save(remind);
                        }
                    }

                    //personnelInfoId 设置为即将到期员工
                    PersonnelInfo personnelInfo=personnelInfoService.get(personnelUse.getPersonnelInfoId());
                    personnelInfo.setExpire(true);
                    personnelInfoService.update(personnelInfo);

                }
            }
        }
    }

    @Override
    public void certValidate() {
        Date today=new Date();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("processState", FlowEnum.ProcessState.Finished);
        map.put("state", BaseEnumManager.StateEnum.Enable);
        List<Certificate> certificateList=certificateService.getList(map);

        for(int i=0;i<certificateList.size();i++){
            Certificate certificate=certificateList.get(i);
            if(DataUtil.DateDiff(certificate.getDatValid(),today)==30){
                certificate.setNumStatus(2);
                certificate.setTxtStatus("到期提醒");
                certificateService.update(certificate);

                Remind remind = new Remind();
                remind.setUser(usersService.get(certificate.getHandlerId()));
                remind.setBussinessKey("certificate");
                remind.setKeyId(certificate.getId());
                remind.setContent("有一份公司资质即将过期，请查看");
                remindService.save(remind);
            }


            if(DataUtil.DateDiff(certificate.getDatValid(),today)==0){
                certificate.setNumStatus(3);
                certificate.setTxtStatus("已失效");
                certificateService.update(certificate);

                Remind remind = new Remind();
                remind.setUser(usersService.get(certificate.getHandlerId()));
                remind.setBussinessKey("certificate");
                remind.setKeyId(certificate.getId());
                remind.setContent("有一份公司资质已经失效，请查看");
                remindService.save(remind);
            }


        }

    }

    @Override
    public void taskWarningDate() {
        Date today=new Date();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("processState", FlowEnum.ProcessState.Finished);
        map.put("state", BaseEnumManager.StateEnum.Enable);
        map.put("txtStatus", "执行中");
        map.put("numStatus", 1);
        List<Task> taskList=taskService.getList(map);

        for(int i=0;i<taskList.size();i++){
            Task task=taskList.get(i);
            if(DataUtil.DateDiff(task.getDatWarning(),today)==0){
                for(String s:task.getLeadersIds().split(",")){
                    Remind remind = new Remind();
                    remind.setUser(usersService.get(s));
                    remind.setBussinessKey("certificate");
                    remind.setKeyId(task.getId());
                    remind.setContent("一个任务即将到期，请查看");
                    remindService.save(remind);
                }
            }
        }
    }
}
